import http from '@/router/http';
import axios from "axios";
const appId = '20250103002244236';
const secret = 'mTOKK6roKArZfsgke3U7';
// const baseUrl = 'https://fanyi-api.baidu.com/api/trans/vip/translate';
const md5 = require('crypto-js/md5');
// 辅助函数，用于生成签名
const generateSign = (appId, q, salt, secret) => {
    return md5(appId + q + salt + secret);
};
export const getTranslationInfo = async (word) => {
    const salt = Math.random();
    const sign = generateSign(appId, word, salt, secret);
    const findValueByKey = (obj, targetKey) => {
        for (const key in obj) {
            if (key === targetKey) {
                return obj[key];
            }
            if (typeof obj[key] === "object" && obj[key]!== null) {
                // 如果属性值是对象且不为null，进一步递归查找
                const result = findValueByKey(obj[key], targetKey);
                if (result!== '') {
                    return result;
                }
            }
        }
        return '';
    };
    try {
        const param = {
            q: word,
            from: 'en',
            to: 'zh',
            appid: appId,
            salt: salt,
            sign: sign.toString()
        }
        const response = await http.post('/student/translate/word',param);
        const basicMeaning = response.data.response.trans_result[0].dst; // 这里简单地将翻译当作基本词义，实际可能需要更准确的提取
        let pronunciation = ''; // 例句
        let audio = '';
        let example = ''; //例句
        let exampleCn = ''; //例句中文
        //发音请求另外的开源接口
        await axios.get('https://api.dictionaryapi.dev/api/v2/entries/en/'+word)
        .then(response => {
            //如果不为空，zeńg，则取第一个，否则取第二个
            if(response.data[0].phonetics[0].text && response.data[0].phonetics[0].text.length>0 ){
                pronunciation = response.data[0].phonetics[0].text;
            }else{
                pronunciation = response.data[0].phonetics[1].text;
            }
            if(response.data[0].phonetics[0].audio && response.data[0].phonetics[0].audio.startsWith('http')){
                audio = response.data[0].phonetics[0].audio;
            }else {
                audio = response.data[0].phonetics[1].audio;
            }
            example = findValueByKey(response.data[0].meanings, "example");
        })
        .catch(error => {
             console.error('获取读音信息出错:', error);
         });
        return {
            pronunciationStr: pronunciation,
            meaning: basicMeaning,
            audio: audio,
            example: example,
            exampleCn: exampleCn
        };
    } catch (error) {
        console.error('获取翻译信息出错:', error);
        return null;
    }
};
export const getTranslationArticleInfo = async (article) => {
    const salt = Math.random();
    const sign = generateSign(appId, article, salt, secret);
    try {
        const param = {
            q: article,
            from: 'en',
            to: 'zh',
            appid: appId,
            salt: salt,
            sign: sign.toString()
        }
        const response = await http.post('/student/translate/word', param);
        const basicMeaning = response.data.response.trans_result[0].dst; // 这里简单地将翻译当作基本词义，实际可能需要更准确的提取
        return {
            meaning: basicMeaning
        };
    } catch (error) {
        console.error('获取翻译信息出错:', error);
        return null;
    }
};
export const getTranslationParaGraphInfo = async (paragraph,index,articleId) => {
    const salt = Math.random();
    const sign = generateSign(appId, paragraph, salt, secret);
    try {
        const param = {
            q: paragraph,
            from: 'en',
            to: 'zh',
            appid: appId,
            salt: salt,
            sign: sign.toString(),
            index: index,
            articleId: articleId
        }
        const response = await http.post('/student/translate/paragraph',param);
        const basicMeaning = response.data.response.trans_result[0].dst; // 这里简单地将翻译当作基本词义，实际可能需要更准确的提取
        return {
            meaning: basicMeaning
        };
    } catch (error) {
        console.error('获取翻译信息出错:', error);
        return null;
    }
};